import api from '../api'; // Ensure this points to your configured Axios instance
import { ElMessage } from 'element-plus';

interface FriendRequestForm {
    requesterId: number;
    receiverId: number;
}
// 添加朋友
export const addFriend = async (form: any) => {
    try {
        const formData = new URLSearchParams();
        formData.append("receiverId", form.receiverId);
        formData.append("requesterId", form.requesterId);

        const { data: response } = await api.post('/friends/add', formData, {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        });
        if (response.status == 200) {
            ElMessage.success(response.message)
        } else if (response.status == 400) {
            ElMessage.warning(response.message)
        }
        return response;
    } catch (error: any) {
        ElMessage.error(error.response.message || 'Failed to send friend request.');
        throw error;
    }
};
// 接受朋友
export const acceptFriendRequest = async (requesterId:number,receiverId:number) => {
    try {
        const url = `/friends/accept?requesterId=${encodeURIComponent(requesterId)}&receiverId=${encodeURIComponent(receiverId)}`;

        const {data:response} = await api.post(url);
        if(response.status==200){
            ElMessage.success(response.message);
        }else if(response.status==400){
            ElMessage.warning(response.message)
        }
        return response;
    } catch (error: any) {
        ElMessage.error(error.response.data.message || 'Failed to accept friend request.');
        throw error;
    }
};
// 获取待接收好友列表
export const getPendingFriendRequests = async (userId: number) => {
    try {
        const response = await api.get(`/friends/pending-requests`, { params: { userId } });
        return response.data;
    } catch (error: any) {
        ElMessage.error(error.response.data.message || 'Failed to retrieve pending friend requests.');
        throw error;
    }
};
// 删除好友
export const deleteFriend = async (form: FriendRequestForm) => {
    try {
        const response = await api.delete('/friends/delete', { data: form });
        ElMessage.success('Friendship deleted successfully.');
        return response.data;
    } catch (error: any) {
        ElMessage.error(error.response.data.message || 'Failed to delete friend.');
        throw error;
    }
};


// 获取所有好友
export const getFriends = async (userId: number) => {
    try {
        const response = await api.get(`/friends/getFriends`, { params: { userId } });
        console.log(response);
        return response.data;
    } catch (error: any) {
        ElMessage.error(error.response.data.message || 'Failed to retrieve friends list.');
        throw error;
    }
};
// 搜索用户
export const searchUsersWithStatus = async (query: string, requesterId: number) => {
    try {
        const response = await api.get(`/friends/searchWithStatus`, {
            params: { query, requesterId }
        });
        return response.data;
    } catch (error: any) {
        ElMessage.error(error.response.data.message || 'Failed to search users.');
        throw error;
    }
};
// 获取在线好友
export const getOnlineFriendList = async (userId: number) => {
    try {
        const {data:response} = await api.get(`/friends/online-friends?userId=${userId}`);
        if(response.sttus=200){
            return response.data;
        }else{
            ElMessage.error("获取在线好友失败")
            return;
        }
        return response.data;
    } catch (error) {
        throw new Error('获取在线好友列表失败');
    }
};